package edu.mse.camel.client.session.actions;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import edu.mse.camel.client.session.plugin.Activator;
import edu.mse.camel.client.session.team.TEAMMEMBER_STATUS;
import edu.mse.camel.client.session.team.TeamList;
import edu.mse.camel.client.session.views.DialogMessages;
import edu.mse.camel.client.session.wizards.CreateSessionWizard;

/**
 * Class associated with the popupMenu for the folder
 * Start the wizard in the run method
 */

public class CreateSessionAction implements IObjectActionDelegate, IViewActionDelegate {

	private IWorkbenchPart part;
	private ISelection selection;

	@Override
	public void init(IViewPart view) {
		this.part = view.getSite().getPart();
		
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 * Instantiates the wizard and opens it in the wizard container
	 */
	public void run(IAction action) {
		
		if (!TeamList.getInstance().getMyStatus().equals(TEAMMEMBER_STATUS.OFFLINE)) {
			if (!TeamList.getInstance().getMyStatus().equals(TEAMMEMBER_STATUS.SESSION)) {
				
				// Instantiates and initializes the wizard
				CreateSessionWizard wizard = new CreateSessionWizard();
				if (part == null) {
					part = 
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
				}
				wizard.init(part.getSite().getWorkbenchWindow().getWorkbench(), 
					(IStructuredSelection) selection);
					
				// Instantiates the wizard container with the wizard and opens it
				WizardDialog dialog = new WizardDialog( new Shell(), wizard);
				dialog.create();
				dialog.open();
				
			} else {
				ErrorDialog ed = new ErrorDialog(new Shell(),
						DialogMessages.DIALOG_ERROR,
						DialogMessages.AlreadyInSession, new Status(
								IStatus.ERROR, Activator.PLUGIN_ID,
								DialogMessages.AlreadyInSession),
						IStatus.ERROR);
				ed.open();				
			}
			
		} else {
			ErrorDialog ed = new ErrorDialog(
					new Shell(),
					DialogMessages.DIALOG_ERROR,
					DialogMessages.CreateSession_LoginError,
					new Status(IStatus.ERROR, Activator.PLUGIN_ID, DialogMessages.CreateSession_LoginError),
					IStatus.ERROR);
			ed.open();
		}

	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.part = targetPart;
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}
}
